package com.hqyj.springboot.modules.account.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.springboot.modules.account.dao.*;

import com.hqyj.springboot.modules.common.vo.Result;
import com.hqyj.springboot.modules.common.vo.Search;
import com.hqyj.springboot.modules.account.entity.Role;
import com.hqyj.springboot.modules.account.service.RoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;


import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleDao2 roleDao2;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private UserRoleDao2 userRoleDao2;
    @Autowired
    private RoleResourceDao2 roleResourceDao2;

    @Override
    @Transactional
    public Page getRolesBySearch(Search search) {
        search.init();
        //设置排序规则
        Sort sort = Sort.by(
                search.getDirection().toLowerCase().equals("asc")
                        ? Sort.Direction.ASC : Sort.Direction.DESC
                , search.getSort());

        //设置分页
        /**
         * PageRequest:它的页码是从0开始的
         */
        PageRequest pageable = PageRequest.of(
                search.getCurrentPage() - 1,
                search.getPageSize(),
                sort);

//        查询的关键字
        Role role = new Role();
        role.setRoleName(search.getKeyword());

        /**
         * withMatcher():设置匹配规则
         * withIgnorePaths():忽略某一个属性
         * matchingAny():or条件查询
         * matchingAll():and条件查询
         */
        ExampleMatcher matcher = ExampleMatcher.matchingAny()
                .withMatcher("roleName",
                        matcher1 -> matcher1.contains())
                .withIgnorePaths("id");

//        根据实体bean的属性以及设定的模板匹配器规则，到数据库查询
        Example<Role> example = Example.of(role, matcher);

        return roleDao.findAll(example,pageable);
    }

    @Override
    @Transactional
    public Result<Role> addRole(Role role) {
        role.setCreateDate(LocalDateTime.now());
        role.setUpdateDate(LocalDateTime.now());
        roleDao2.addRole(role);

        return new Result<Role>(Result.ResultStatus.SUCCESS.code, "角色添加成功!",role);

    }

    @Override
    @Transactional
    public Result<Role> updateRole(Role role) {
        role.setUpdateDate(LocalDateTime.now());
         roleDao2.updateRole(role);
         return new Result<Role>(Result.ResultStatus.SUCCESS.code, "角色修改成功!",role);
    }

    @Override
    @Transactional
    public Result<Object> deleteRoleById(int id) {
        roleDao2.deleteRoleById(id);
        userRoleDao2.deleteUserRoleByRoleId(id);
        roleResourceDao2.deleteRoleResourceByRoleId(id);
        return new Result<Object>(Result.ResultStatus.SUCCESS.code, "角色删除成功!");


    }

    @Override
    public Role getRoleById(int id) {
        return roleDao2.getRoleByRoleId(id);
    }

    @Override
    public List<Role> getRolesByUserId(int userId) {
        return Optional.ofNullable(roleDao2.getRolesByUserId(userId)).orElse(Collections.emptyList());
    }

    @Override
    public List<Role> getRoles() {
        return roleDao.findAll();
    }

    @Override
    public PageInfo<Role> getRolesBySearchBean(Search search) {
        search.init();
        PageHelper.startPage(search.getCurrentPage(),search.getPageSize());
        return new PageInfo<Role>(Optional.ofNullable(roleDao2.getRolesBySearch(search))
                .orElse(Collections.emptyList()));
    }


}
